﻿/*
=======================================================
    UsingWireropeKeep【在用绳维保】
=======================================================
*/

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Services
{
    public interface IUsingWireropeKeepLogic : ILogic<UsingWireropeKeepModel, UsingWireropeKeepFilterModel>
    {
    }


    public class UsingWireropeKeepLogic : BaseLogic<UsingWireropeKeepModel, UsingWireropeKeepFilterModel, UsingWireropeKeep, IUsingWireropeKeepRepository, IUsingWireropeKeepMapping, IUsingWireropeKeepService>, IUsingWireropeKeepLogic
    {
        private readonly IUsingWireropeRepository _IUsingWireropeRepository;
        private readonly IUsingWireropeMapping _IUsingWireropeMapping;

        public UsingWireropeKeepLogic(ICurrentUser currentUser, IUsingWireropeKeepRepository repository, IUsingWireropeKeepMapping mapping, IUsingWireropeKeepService service, IUsingWireropeRepository __IUsingWireropeRepository, IUsingWireropeMapping __IUsingWireropeMapping)
            : base(currentUser, repository, mapping, service)
        {
            _IUsingWireropeRepository = __IUsingWireropeRepository;
            _IUsingWireropeMapping = __IUsingWireropeMapping;
        }

        #region Insert
        public override UsingWireropeKeepModel Insert(UsingWireropeKeepModel model)
        {
            model.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.CreateDate = DateTime.Now;
            model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;

            return base.Insert(model);
        }
        #endregion

        #region GetPage
        public override PageModel<UsingWireropeKeepModel> GetPage(UsingWireropeKeepFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            PageModel<UsingWireropeKeepModel> pageModel = new PageModel<UsingWireropeKeepModel>();
            var (items, totalCount) = _repository.GetPage(_mapping.FilterModel2Where(filterModel), pageIndex, pageSize, orderBy);
            pageModel.TotalCount = totalCount;
            var ItemsTemp = _mapping.Entity2Model(items);

            var ids = ItemsTemp.Select(x => x.UsingWireropeID).ToList();

            var where = Expressionable.Create<UsingWirerope>();
            where = where.And(x => ids.Contains(x.UsingWireropeID));
            var list = _IUsingWireropeMapping.Entity2Model(_IUsingWireropeRepository.GetSearch(where));

            foreach (var item in ItemsTemp)
            {
                item.UsingWireropeModel = list.FirstOrDefault(x => x.UsingWireropeID == item.UsingWireropeID);
            }

            pageModel.Items = ItemsTemp;

            return pageModel;
        }
        #endregion

        #region GetByID
        public override UsingWireropeKeepModel GetByID(int id)
        {
            var model = GetModel(new UsingWireropeKeepFilterModel { _UsingWireropeKeepID = id });

            var where2 = Expressionable.Create<UsingWirerope>();
            where2 = where2.And(x => x.UsingWireropeID == model.UsingWireropeID);
            model.UsingWireropeModel = _IUsingWireropeMapping.Entity2Model(_IUsingWireropeRepository.GetModel(where2));

            return model;
        } 
        #endregion
    }
}
